#!/bin/sh

HTTPD_ROOTDIR='/var/www'

RRDTOOL='/usr/local/bin/rrdtool
/usr/local/bin/rrdcgi
/usr/local/bin/rrdupdate'

echo "Put rrdtool into chrooted apache."

echo "Create necessary directories."
mkdir -p ${HTTPD_ROOTDIR}/{bin,usr/lib,usr/libexec} 2>/dev/null

echo "Copy necessary libraries."
for i in ${RRDTOOL}
do
    FILES=$(ldd $i | grep '/usr/' | awk '{print $NF}' |grep -v ':$' | grep -v $i)

    for j in ${FILES}
    do
        #DIR_IN_CHROOTED="${HTTPD_ROOTDIR}/$(dirname $j)"
        #echo ${DIR_IN_CHROOTED}

        #echo "Create directory: ${DIR_IN_CHROOTED}."
        #mkdir -p ${DIR_IN_CHROOTED} 2>/dev/null

        #echo "Copy file: $j -> $DIR_IN_CHROOTED."
        #cp -rf $j ${DIR_IN_CHROOTED}

        #echo ""

        cp $j ${HTTPD_ROOTDIR}/usr/lib/
    done

    cp $i ${HTTPD_ROOTDIR}/bin/
done

echo "Copy misc files."
cp -f /usr/libexec/ld.so ${HTTPD_ROOTDIR}/usr/libexec
#cp -f /bin/sh ${HTTPD_ROOTDIR}/bin/sh

echo "Done."
